faApp.controller('SimuListCtrl',['$scope','simuService','$location','$controller','$route',function($scope,simuService,$location,$controller,$route) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.list = [];
  $scope.loading = true;
 
  $scope.$watch('config',function(){
    simuService.list($scope.config).then(function(json){
      $scope.list = json.list;
      $scope.pager = json.pager;
      $scope.loading = false;
    });
  },true)

  $scope.config = {
    "pageSize" : 20,
    "pageNo" : 1
  };
  
  $scope.pageChanged = function(){
    $scope.config.pageNo = $scope.pager.pageNo;
  };   

  $scope.searchMap = {
    "jjmc" : '', 
  };

  $scope.search = function(){

    angular.forEach($scope.searchMap,function(o,i){
      $scope.config[i] = ( undefined == $scope.searchMap[i] ? "" : $scope.searchMap[i]);
    });

    $scope.config.pageNo = 1;

  };

  $scope.changeWeightMap = {};
  $scope.productWeightMap = {};
  $scope.showChangeWeight = function(id){
    if (!$scope.hasAuth("btn","PRODUCT_RECOMMEND")){
      return;
    }
    $scope.changeWeightMap[id] = true;
  };
  $scope.changeWeight = function(id){
    simuService.productRecommend({fpProjectId:id,recommendWeight:$scope.productWeightMap[id],fpProjectType:'01'}).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("修改推荐状态成功！");
        $route.reload();
      }
    });
  };
  
  // $scope.remove = function(id){
  //   simuService.remove({id:id}).then(function(json){
  //     if (json.statusCode){
  //       alert("出错了:" + json.statusCode);
  //     }else{
  //       $route.reload();
  //     }
  //   });
  // };
  
 /* $scope.stop = function(id,jjbm){
    simuService.stop({id:id,jjbm:jjbm}).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $route.reload();
      }
    });
  };
  
  $scope.start = function(id,jjbm){
    simuService.start({id:id,jjbm:jjbm}).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $route.reload();
      }
    });
};*/
}]);

faApp.controller('SimuBaseInfoCtrl',['$scope','$location','$controller','$routeParams','$timeout',function($scope,$location,$controller,$routeParams,$timeout) {   
  $controller('BaseCtrl', {$scope: $scope});
  $scope.id=$routeParams.id;
  $scope.jjbm=$routeParams.jjbm;
  $scope.pathText="产品详情";
  $scope.tabUrlMap = {
    viewUrl : "#/simu/view?id=" + $scope.id,
    baseUrl : "#/simu/edit?id=" + $scope.id,
    buyinfoUrl : "#/simu/buyinfo/edit?id=" + $scope.id + "&jjbm=" + $scope.jjbm,
    buythisUrl : "#/simu/buyhis/addlist?id=" + $scope.id+"&jjbm=" + $scope.jjbm,
    lsyjUrl : "#/simu/lsyj?id=" + $scope.id,
    ndyjUrl : "#/simu/ndyj?id=" + $scope.id,
    lsjzUrl : "#/simu/lsjz?id=" + $scope.id,
    spnrUrl : "#/simu/spnr?id="+$scope.id + "&jjbm=" + $scope.jjbm,
  };
  $scope.tabUrl={};
  $scope.changeTab = function(tab){
    var tmp = tab.split(",");
    for(var i=0;i<tmp.length;i++){
      $scope.tabUrl[tmp[i]] = $scope.tabUrlMap[tmp[i]];
    }
    //$scope.tabUrl['ndyjUrl'] = $scope.tabUrlMap['ndyjUrl'];
  };
  
  $scope.changeTab('viewUrl');
  
}]);

faApp.controller('SimuAddCtrl',['$scope','simuService','simuCompanyService','simuManagerService','$location','$controller','$timeout','$routeParams',function($scope,simuService,simuCompanyService,simuManagerService,$location,$controller,$timeout,$routeParams) {   
	$controller('BaseCtrl', {$scope: $scope});

  $scope.item = {};
  $scope.loading = true; 
  $scope.simuCompanyListing = [];
  $scope.simuCompanyListed = [];
  $scope.simuCurCompanyListed = [];
  $scope.companyKw = "";
  $scope.allTjzls = {};
  if($routeParams.outProductId!=undefined){
    $scope.item.outProductId=$routeParams.outProductId;
  }
  if($routeParams.productName!=undefined){
    $scope.item.jjqc=$routeParams.productName;
    $scope.item.jjmc=$routeParams.productName;
    
  }
  $scope.$watch('companyKw',function(){
    $scope.companyKw = $scope.companyKw.trim();
    if ($scope.companyKw){
      simuCompanyService.list({pageSize:20,pageNo:1,gsmc:$scope.companyKw}).then(function(json){
        $scope.simuCompanyListing = json.list;
      });
    }else{
      $scope.simuCompanyListing = [];
    }
  });
  
  $scope.selectCompany = function(id,idx,isCur){
    angular.forEach($scope.simuCompanyListing,function(o){
      if (o.id == id){
        var existFlag = false;
        angular.forEach($scope.simuCompanyListed,function(oo){
          if (oo.id == id)existFlag = true;
        });
        if (!existFlag) {
          if (isCur){
            $scope.simuCurCompanyListed = [o];
            $scope.item.jjgsId = o.id;
            $scope.item.jjgs = o.gsmc;
          }else{
            $scope.simuCompanyListed.push(o);
          }
        }
      }
    });
  };
  
  $scope.unSelectCompany = function(id,idx,isCur){
    if (isCur){
      $scope.simuCurCompanyListed = [];
      $scope.item.jjgsId = "";
      $scope.item.jjgs = "";
    }else{
      $scope.simuCompanyListed.splice(idx,1);
    }
  };
  

  $scope.simuManagerListing = [];
  $scope.simuManagerListed1 = [];
  $scope.simuManagerListed2 = [];
  $scope.managerKw = "";
  
  $scope.$watch('managerKw',function(){
    $scope.managerKw = $scope.managerKw.trim();
    if ($scope.managerKw){
      simuManagerService.list({pageSize:20,pageNo:1,jjjlmc:$scope.managerKw}).then(function(json){
        $scope.simuManagerListing = json.list;
      });
    }else{
      $scope.simuManagerListing = [];
    }
  });
  
  $scope.selectManager = function(id,idx,isCur){
    angular.forEach($scope.simuManagerListing,function(o){
      if (o.id == id){ 
        if (isCur){
          $scope.simuManagerListed1 = [o];
          $scope.item.jjjlbm = o.jjjlbm;
        }else{
          $scope.simuManagerListed2 = [o];
          $scope.item.jjjlbm2 = o.jjjlbm;
        } 
      }
    });
  };
  
  $scope.unSelectManager = function(id,idx,isCur){
    if (isCur){
      $scope.simuManagerListed1 = [];
      $scope.item.jjjlbm = "";
    }else{
      $scope.simuManagerListed2 = [];
      $scope.item.jjjlbm2 = "";
    }
  };

  $scope.getJJBM=function(){
    // var v;
    // if($scope.item.jjfl=='股票型'){
    //   v="GP";
    // }else if($scope.item.jjfl=='债券型'){
    //   v="ZJ";
    // }else if($scope.item.jjfl=='期货型'){
    //   v="QH";
    // }else if($scope.item.jjfl=='混合型'){
    //   v="HH";
    // }else{
    //   v="QT";
    // }
    // if(v!=undefined){
    simuService.queryjjbm().then(function(json){
       $scope.item.jjbm=json.code;
    });
    // }
  };
  $scope.getJJBM();
  $scope.save = function(){
    if ($scope.form.$invalid){
      alert("请先完整填写表单");
      return;
    }else if($scope.simuCurCompanyListed==""||$scope.simuManagerListed1==""){
      alert("基金公司和基金经理必填");
      return;
    }else{
      $scope.selectTjzls = [];
     var flag = false;
      for (var i in $scope.allTjzls){
        if ($scope.allTjzls[i]){
           flag = true;
           $scope.selectTjzls.push(i);
        }
      }
      if (!flag){
        alert("请勾选提交资料");
        return;
      }
      $scope.item.tjzls=$scope.selectTjzls.join(",");
      if ($scope.item.jjclsjstr){$scope.item.jjclsj = $scope.item.jjclsjstr + " 00:00:00";}
      simuService.add($scope.item).then(function(json){
        if (json.statusCode){
          if(json.statusCode=='2003'){
            alert("基金编码重复");
          }else{
            alert("报错了:"+json.statusCode);
          }
        }else{
          $location.path("/simu/list");
        }
      });
    }
  }; 

}]);

faApp.controller('SimuEditCtrl',['$scope','simuCompanyService','simuManagerService','simuService','$location','$controller','$routeParams','$route','$filter',function($scope,simuCompanyService,simuManagerService,simuService,$location,$controller,$routeParams,$route,$filter) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.item = {};
  $scope.loading = true; 
  $scope.id = $routeParams.id;
  $scope.simuCompanyListing = [];
  $scope.simuCompanyListed = [];
  $scope.simuCurCompanyListed = [];
  $scope.companyKw = "";
  $scope.allTjzls = {};
  
  $scope.$watch('companyKw',function(){
    $scope.companyKw = $scope.companyKw.trim();
    if ($scope.companyKw){
      simuCompanyService.list({pageSize:20,pageNo:1,gsmc:$scope.companyKw}).then(function(json){
        $scope.simuCompanyListing = json.list;
      });
    }else{
      $scope.simuCompanyListing = [];
    }
  });
  
  $scope.selectCompany = function(id,idx,isCur){
    angular.forEach($scope.simuCompanyListing,function(o){
      if (o.id == id){
        var existFlag = false;
        angular.forEach($scope.simuCompanyListed,function(oo){
          if (oo.id == id)existFlag = true;
        });
        if (!existFlag) {
          if (isCur){
            $scope.simuCurCompanyListed = [o];
            $scope.item.jjgsId = o.id;
            $scope.item.jjgs = o.gsmc;
          }else{
            $scope.simuCompanyListed.push(o);
          }
        }
      }
    });
  };
  
  $scope.unSelectCompany = function(id,idx,isCur){
    if (isCur){
      $scope.simuCurCompanyListed = [];
      $scope.item.jjgsId = "";
      $scope.item.jjgs = "";
    }else{
      $scope.simuCompanyListed.splice(idx,1);
    }
  };
  

  $scope.simuManagerListing = [];
  $scope.simuManagerListed1 = [];
  $scope.simuManagerListed2 = [];
  $scope.managerKw = "";
  
  $scope.$watch('managerKw',function(){
    $scope.managerKw = $scope.managerKw.trim();
    if ($scope.managerKw){
      simuManagerService.list({pageSize:20,pageNo:1,jjjlmc:$scope.managerKw}).then(function(json){
        $scope.simuManagerListing = json.list;
      });
    }else{
      $scope.simuManagerListing = [];
    }
  });
  
  $scope.selectManager = function(id,idx,isCur){
    angular.forEach($scope.simuManagerListing,function(o){
      if (o.id == id){ 
        if (isCur){
          $scope.simuManagerListed1 = [o];
          $scope.item.jjjlbm = o.jjjlbm;
        }else{
          $scope.simuManagerListed2 = [o];
          $scope.item.jjjlbm2 = o.jjjlbm;
        } 
      }
    });
  };
  
  $scope.unSelectManager = function(id,idx,isCur){
    if (isCur){
      $scope.simuManagerListed1 = [];
      $scope.item.jjjlbm = "";
    }else{
      $scope.simuManagerListed2 = [];
      $scope.item.jjjlbm2 = "";
    }
  };

  simuService.query({id:$scope.id}).then(function(json){
    $scope.item = json.detail || {};
    $scope.item.zdrgje=parseInt($scope.item.zdrgje);
    $scope.item.zjzdje=parseInt($scope.item.zjzdje);
    $scope.item.jjclsjstr = $filter("date")($scope.item.jjclsj,"yyyy-MM-dd");
    $scope.simuCurCompanyListed = [json.company];
    if (json.manager && json.manager.jjjlmc)$scope.simuManagerListed1 = [json.manager];
    if (json.manager && json.manager.jjjlmc)$scope.simuManagerListed2 = [json.manager2];
    delete $scope.item.jjclsj;
    delete $scope.item.lsjzJson;
    delete $scope.item.lsyjbJson;
    delete $scope.item.ndyjbJson;
    if($scope.item.tjzls!=undefined){
      angular.forEach($scope.item.tjzls.split(","),function(o){
        $scope.allTjzls[o] = true;  
      }); 
    }
    $scope.loading = false;
  });

  $scope.save = function(){
    if ($scope.form.$invalid){
      alert("请先完整填写表单");
    }else if($scope.simuCurCompanyListed==""||$scope.simuManagerListed1==""){
      alert("基金公司和基金经理必填");
      return;
    }else{
      $scope.selectTjzls = [];
      var flag = false;
      for (var i in $scope.allTjzls){
        if ($scope.allTjzls[i]){
           flag = true;
           $scope.selectTjzls.push(i);
        }
      }
      if (!flag){
        alert("请勾选提交资料");
        return;
      }
      $scope.item.tjzls=$scope.selectTjzls.join(",");
      if ($scope.item.jjclsjstr){
        $scope.item.jjclsj = $scope.item.jjclsjstr + " 00:00:00";
      }else{
        $scope.item.jjclsj = "";
      }
      delete $scope.item.fpFundBuynoticeDTO;
      simuService.edit($scope.item).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          // $location.path("/simu/list");
          alert("修改成功");
          $route.reload();
        }
      });
    } 
  };


}]);

faApp.controller('SimuViewCtrl',['$scope','simuService','productService','userService','$location','$controller','$route','$routeParams','$sce',function($scope,simuService,productService,userService,$location,$controller,$route,$routeParams,$sce) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.id = $routeParams.id;
  $scope.item = {};
  $scope.loading = true;
  $scope.userList = [];
    userService.userList().then(function(json){
      $scope.userRawList = json.list || [];
      angular.forEach($scope.userRawList,function(o){
        if (o.userNickname != $scope.loginStatus.loginName){
          $scope.userList.push(o);
        }
    });
  });
  $scope.transList = [];
  productService.transPermissionList({pProjectId:$scope.id,pProjectType:'02',pageSize:1000}).then(function(json){
    $scope.transList = json.list || [];
  });

  simuService.query({id:$scope.id}).then(function(json){
    $scope.item = json.detail || {};
    $scope.company = json.company|| {};
    $scope.manager = json.manager || {};
    $scope.manager2 = json.manager2 || {};
    //前台html展示
    $scope.item.fpFundBuynoticeDTO.buyNoticeDetail=$sce.trustAsHtml($scope.item.fpFundBuynoticeDTO.buyNoticeDetail);
    var zdf=angular.fromJson(App.filterSimuJson(json.detail.lsjzJson || "[]"));
    $scope.item.lsjz=zdf[0]||{};
    $scope.loading = false;
  });
  $scope.stop = function(id,jjbm){
    simuService.stop({id:id,jjbm:jjbm}).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $route.reload();
      }
    });
  };
  
  $scope.start = function(id,jjbm){
    simuService.start({id:id,jjbm:jjbm}).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $route.reload();
      }
    });
  };

  $scope.remove = function(id){
    simuService.remove({id:id}).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $location.path("/simu/list");
      }
    });
  };

  $scope.saveTrans = function(){
    if(!$scope.form.$invalid){
      $scope.trans.pProjectId=$scope.id;
      $scope.trans.pProjectType='02';
      productService.transPermissionAdd($scope.trans).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          $scope.alertNextPage("操作成功");
          parent.location.href = parent.location.href;
          //$route.reload();
        }
      });
    }else{
      alert("请先完整填写表单");
    }
  };

}]);
